#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: rename.sh
# Description: 
#   Linux Rename Multiple Files At a Shell Prompt
# Edit History: 
#   2011-03-28    File created.
#========================================================================

#Usage:
#  rename "regex-rule" files 
#  rename oldname newname *.files

mkdir rename
touch rename/"06 -  Gorillaz - Feel Good Inc.mp3"
touch rename/"DDR - Kung- Fu Fighting (bus stop).mp3"
touch rename/"AXEL CRAZYFROG.mp3"
#Ex1: Remove all blank space with rename command
ls rename
#Output:
# 06 -  Gorillaz - Feel Good Inc.mp3
# DDR - Kung- Fu Fighting (bus stop).mp3
# AXEL CRAZYFROG.mp3

rename "s/ *//g" rename/*.mp3
ls rename
rm rename/*
#Output:
# 06-Gorillaz-FeelGoodInc.mp3
# DDR-Kung-FuFighting(busstop).mp3
# AXEL-CRAZYFROG.mp3

#Ex2: To remove .jpg file extension, you write command as follows:
#$ rename 's/\.jpg$//' *.jpg

#Ex3: To convert all uppercase filenames to lowercase:
#$ rename 'y/A-Z/a-z/' *

#Before using the rename command I was using the following shell script to rename my mp3s
# To remove blank space
if [ $# -eq 0 ];
then
	echo "Syntax: $(basename $0) file-name [command]"
	exit 1
fi
FILES=$1
CMD=$2
for i in $FILES
do
	# remove all blanks and store them OUT
	OUT=$(echo $i | sed 's/  *//g')
	if [ "$CMD" == "" ];
	then
		#just show file
		echo $OUT
	else
		#else execute command such as mv or cp or rm
		[ "$i" != "$OUT" ] && $($CMD  "$i"  "$OUT")
	fi
done
